1   // Copyright 2009-2013 The Apache Software Foundation
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   // http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  
15  package org.apache.tapestry5.integration.app1;
16  
17  import org.apache.tapestry5.internal.TapestryInternalUtils;
18  import org.apache.tapestry5.test.TapestryRunnerConstants;
19  import org.testng.annotations.DataProvider;
20  import org.testng.annotations.Test;
21  
22  import java.io.BufferedInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.io.InputStream;
26  import java.net.URL;
27  
28  public class AssetTests extends App1TestCase
29  {
30      @DataProvider
31      private Object[][] asset_data()
32      {
33          return new Object[][]{
34                  {"icon", "src/test/app1/images/tapestry_banner.gif"},
35                  {"button", "src/test/resources/org/apache/tapestry5/integration/app1/pages/nested/tapestry-button.png"},
36                  {"viaContext", "src/test/app1/images/asf_logo_wide.gif"},
37                  {"meta", "src/test/resources/META-INF/assets/tapestry.png"},
38                  {"templatemeta", "src/test/resources/META-INF/assets/plugin.png"}};
39      }
40  
41      @Test(dataProvider = "asset_data")
42      public void assets(String id, String localPath) throws Exception
43      {
44          openLinks("AssetDemo");
45  
46          // Test for https://issues.apache.org/jira/browse/TAPESTRY-1935
47  
48          // assertSourcePresent("<link href=\"/css/app.css\" rel=\"stylesheet\" type=\"text/css\">");
49  
50          // Read the byte stream for the asset and compare to the real copy.
51  
52          String assetURL = getAttribute(String.format("//img[@id='%s']/@src", id));
53  
54          compareDownloadedAsset(assetURL, localPath);
55      }
56      
57      // TAP5-1515
58      @Test
59      public void external_url_asset_bindings()
60      {
61          openLinks("AssetDemo");
62          
63          assertEquals("http://cdnjs.cloudflare.com/ajax/libs/d3/3.0.0/d3.js", getText("httpAsset"));
64          assertEquals("https://cdnjs.cloudflare.com/ajax/libs/d3/3.0.0/d3.js", getText("httpsAsset"));
65          assertEquals("http://cdnjs.cloudflare.com/ajax/libs/d3/3.0.0/d3.js", getText("protocolRelativeAsset"));
66          assertEquals("ftp://cdnjs.cloudflare.com/ajax/libs/d3/3.0.0/d3.js", getText("ftpAsset"));
67          
68          // check whether externaly @Import'ed d3 works
69          assertTrue(isElementPresent("css=svg"));
70      }
71      
72      // TAP5-2185
73      @Test
74      public void redirection_of_requests_to_assets_with_wrong_checksums()
75      {
76          openLinks("AssetDemo");
77          // paragraph is rendered with display="none" and the javascript asset changes it to display="block"
78          // without the fix, selenium timesout because the javascript code that sets the condition
79          // used by tapestry testing code to know when the page is finished loading is never invoked.
80          assertTrue(isVisible("assetWithWrongChecksum"));
81      }
82  
83  
84      private void compareDownloadedAsset(String assetURL, String localPath) throws Exception
85      {
86          // Strip off the leading slash
87  
88          URL url = new URL(getBaseURL() + assetURL.substring(1));
89  
90          byte[] downloaded = readContent(url);
91  
92          File local = new File(TapestryRunnerConstants.MODULE_BASE_DIR, localPath);
93  
94          byte[] actual = readContent(local.toURL());
95  
96          assertEquals(downloaded, actual);
97      }
98  
99      private byte[] readContent(URL url) throws Exception
100     {
101         InputStream is = new BufferedInputStream(url.openStream());
102 
103         ByteArrayOutputStream os = new ByteArrayOutputStream();
104 
105         TapestryInternalUtils.copy(is, os);
106 
107         os.close();
108         is.close();
109 
110         return os.toByteArray();
111     }
112 }